import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import reseau.session.client.SessionG_TCP_Client;
import exception.NTWException;

public class StubClient extends JFrame implements Runnable {

    private static SessionG_TCP_Client session;
    private JPanel                     contentPane;
    private JSpinner                   spinner;

    /**
     * Launch the application.
     * 
     * @throws NTWException
     */
    public static void main(String[] args) throws NTWException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    StubClient frame = new StubClient();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * 
     * @throws NTWException
     */
    public StubClient() throws NTWException {
        session = new SessionG_TCP_Client("Client boulet", "localhost", 3600);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnEncherir = new JButton("Encherir");
        btnEncherir.addActionListener(new BtnEncherirActionListener());
        btnEncherir.setBounds(134, 28, 290, 56);
        contentPane.add(btnEncherir);

        spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(new Long(0), null, null,
                new Long(1)));
        spinner.setBounds(5, 28, 105, 56);
        contentPane.add(spinner);

        JButton btnDeclarer = new JButton("Declarer");
        btnDeclarer.addActionListener(new BtnDeclarerActionListener());
        btnDeclarer.setBounds(184, 110, 240, 51);
        contentPane.add(btnDeclarer);

        new Thread(this).start();
    }

    private class BtnDeclarerActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {

            HashMap msg = new HashMap();
            msg.put("cmd", "declarer");
            msg.put("nom", "Client boulet");
            session.deposerMessage(msg);

        }
    }

    private class BtnEncherirActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            HashMap msg = new HashMap();
            msg.put("cmd", "encherir");
            msg.put("nom", "Client boulet");
            msg.put("prix", spinner.getValue());
            session.deposerMessage(msg);

        }
    }

    @Override
    public void run() {
        while (true) {
            session.attendreMessage();
            System.out.println(session.retirerMessage());
        }
    }
}
